<?php
include "config.php";
include "thu_vien/database.php";
$mysql = new database;
/*
 $scope = 'user_about_me,user_activities,user_birthday,user_checkins,user_education_history,user_events,user_groups,user_hometown,user_interests,user_likes,user_location,user_notes,user_photos,user_questions,user_relationships,user_relationship_details,user_religion_politics,user_status,user_videos,user_website,user_work_history,email,user_photos,friends_photos,user_events,friends_events,friends_about_me,friends_activities,friends_birthday,friends_checkins,friends_education_history,friends_events,friends_groups,friends_hometown,friends_interests,friends_likes,friends_location,friends_notes,friends_photos,friends_questions,friends_relationships,friends_relationship_details,friends_religion_politics,friends_status,friends_videos,friends_website,friends_work_history,read_friendlists,read_insights,read_mailbox,read_requests,read_stream,xmpp_login,ads_management,manage_friendlists,manage_notifications,user_online_presence,friends_online_presence,publish_checkins';
 */
    $scope ='email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,friends_about_me,friends_activities,friends_birthday,friends_checkins,friends_education_history,friends_events,friends_groups,friends_hometown,friends_interests,friends_likes,friends_location,friends_notes,friends_photos,friends_questions,friends_relationships,friends_relationship_details,friends_religion_politics,friends_status,friends_videos,friends_website,friends_work_history,read_friendlists';
 
include "thu_vien/facebook.php";

/*
$facebook = new Facebook(array(
  'appId' => '422052821143625',
  'secret' => 'e57e73495992f3ef2ec8581e9e9d2a7f' ,
  'cookie' => false,
));
*/

/**
 * @author Steve Smith
 * @copyright 2012
 */
/*
 $user = $facebook->getUser();
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
    echo '<pre>';
  print_r("query results:");
  print_r($user_profile);
  echo '</pre>';
  
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
} else { 
    echo "vao day";
    $loginUrl = $facebook->getLoginUrl(
                                array(
                                'scope' => $scope,
                                'redirect_uri' => $my_url,
                                    ));
                                    
    $user = $facebook->getUser();        
     $user_profile = $facebook->api('/me');
    echo '<pre>';
  print_r("query results:");
  print_r($user_profile);
  echo '</pre>';              
}

*/
  $app_id = '422052821143625';
  $app_secret = 'e57e73495992f3ef2ec8581e9e9d2a7f';
  //$app_id = $facebook->getAppId();
  //$app_secret = $facebook->getApiSecret();
  $my_url = 'http://localhost:88/fb/sample.php';

  if(isset($_REQUEST['code']))
    $code = $_GET['code'];
 
 //auth user
 if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
    . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  //get user access_token
  $token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
    . $app_id .'&redirect_uri=' . urlencode($my_url)
	.'&scope=' . $scope
    . '&client_secret=' . $app_secret 
    . '&code=' . $code;
		
  $access_token = file_get_contents($token_url);
  
  //  $facebook->setAccessToken($access_token);
/*
    $fql = "SELECT uid2 FROM friend WHERE uid1 = $uid";
        $friend_list = $facebook->api(array(
                                  'method' => 'fql.query',
                                   'query' => $fql,
                                 ));
*/

    // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid,username,name,pic,profile_update_time,timezone,religion,birthday,birthday_date,sex,hometown_location,meeting_sex,meeting_for,relationship_status,significant_other_id,political,current_location,activities,interests,is_app_user,music,tv,movies,books,quotes,about_me,notes_count,wall_count,status,locale,profile_url,pic_with_logo,allowed_restrictions,verified,games,work,education,sports,favorite_athletes,favorite_teams,likes_count,friend_count,can_post,mutual_friend_count,contact_email,email+FROM+user+WHERE+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1=me()+Limit+10)'
    . '&' . $access_token;
    
    /*
    $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid,name,username,quotes,sex,timezone,locale,verified,profile_update_time+FROM+user+WHERE+uid=me()+OR+uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1=me())'
    . '&' . $access_token;
	*/
/*    
   $fql_query_url = 'https://graph.facebook.com/'
   .'/fql?q=select+gid,uid+from+group_member+where+gid=240461512671914+limit+50'
    //.'/fql?q=SELECT+uid,name,friend_count,pic_square+FROM+user+WHERE+'//uid=me()+OR+' 
    //.'uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1=me())' //lay thong tin uid b?n bè
    .'&' . $access_token;
    */
  /*   
    $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+'
    .'uid,username,first_name,middle_name,last_name,sex,birthday_date,profile_url,timezone,email'
    .'+FROM+user+'
    .'WHERE+uid=me()'
    . '&' . $access_token;
*/    
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  
  
$friend_ds = $fql_query_obj['data'];
  foreach($friend_ds as $friend){ 
		$fid = $friend['uid']; 
		$f_uname = $friend['username'];
		$f_name = $friend['name'];
		$f_pic = $friend['pic']; 
		$f_prof_updtime = $friend['profile_update_time'];
		$f_timezone = $friend['timezone']; 
		$f_religino = $friend['religion']; 
		$f_btday = $friend['birthday_date'];
		$f_sex = $friend['sex']; 
		$f_hometown = $friend['hometown_location']['name'].','.$friend['hometown_location']['country']; 
		$f_meeting_sex = $friend['meeting_sex'];
		$f_meeting_for = $friend['meeting_for'];
		$f_relationship_status = $friend['relationship_status'];
		$f_significant_other_id = $friend['relationship_status']; 
		$f_political = $friend['political']; 
		$f_current_location =$friend['current_location']['name'].','.$friend['current_location']['country'];  
		$f_activities = $friend['activities']; 
		$f_interests = $friend['interests']; 
		$f_music = $friend['music'];
		$f_tv = $friend['tv']; 
		$f_movies = $friend['movies']; 
		$f_books = $friend['books']; 
		$f_quotes = $friend['quotes']; 
		$f_about_me = $friend['about_me']; 
		$f_notes_count = $friend['notes_count']; 
		$f_wall_count = $friend['wall_count'];
		$f_status = $friend['status']['message']; 
		$f_locale = $friend['locale']; 
		$f_profile_url = $friend['profile_url']; 
		$f_pic_logo = $friend['pic_with_logo']; 
		$f_allowed_restrictions = $friend['allowed_restrictions']; 
		$f_verified = $friend['verified']; 
		$f_games = $friend['games']; 
		//$f_work = $friend['work'];/////thieu
		$f_education = 0 ; ///thieu
		//$f_sport = 0; ///thieeu
		$f_favorite_athletes = 0;//thieu
		$f_likes_count = $friend['likes_count'];
		$f_friend_count = $friend['friend_count']; 
		$f_can_post = $friend['can_post']; 
		$f_mutual_friend_count = $friend['mutual_friend_count']; 
		
		
		
		
		$sql = "INSERT INTO `user` (`uid`, `name`, `pic`, `link`, `username`, `bio`, `quote`, `sex`, `timezone`, `religion`, `birthday`, `hometown_location`, `meeting_sex`, `meeting_for`, `relationship_status`, `significant_other_id`, `political`, `current_location`, `activities`, `interests`, `music`, `tv`, `movies`, `books`, `about_me`, `notes_count`, `wall_count`, `status`, `profile_url`, `pic_with_logo`, `allowed_restrictions`, `website`, `is_blocked`, `contact_email`, `email`, `games`, `work`, `education`, `sports`, `favorite_athletes`, `favorite_teams`, `languages`, `likes_count`, `friend_count`, `mutual_friend_count`, `can_post`, `locale`, `verified`, `profile_update_time`) VALUES ('$fid', '".urlencode($f_name)."', '$f_pic', '$f_profile_url', '".urlencode($f_uname)."', '', '".urlencode($f_quotes)."', '$f_sex', '$f_timezone', '$f_religino', '$f_btday', '$f_hometown', '$f_meeting_sex', '$f_meeting_for', '$f_relationship_status', '$f_significant_other_id', '$f_political', '$f_current_location', '".urlencode($f_activities)."', '$f_interests', '".urlencode($f_music)."', '$f_tv', '$f_movies', '".urlencode($f_books)."', '".urlencode($f_about_me)."', '$f_notes_count', '$f_wall_count', '".urlencode($f_status)."', '$f_profile_url', '$f_pic_logo', '$f_allowed_restrictions', '', '', '', '', '$f_games', '', '', '', '', '', '', '$f_likes_count', '$f_friend_count', '$f_mutual_friend_count', '$f_can_post', '','$f_verified', '$f_prof_updtime');";
		
		echo $sql; exit;
		$mysql->setQuery($sql);
		$mysql->query();
		$error = $mysql->get_error();
		if($error != '')
		{	
			echo "có lỗi";
			exit;
		}		
  }
echo "thang cong";
exit;
//display results of fql query
  echo '<pre>';
  print_r("query results:");
  print_r($fql_query_obj);
  echo '</pre>';




//l?y b?n c?a b?n
/*
$i =0;
$friend_ds = $fql_query_obj['data'];
  foreach($friend_ds as $friend){  
      $fid = $friend['uid'];
      $count = (string)$fid;
        //echo "</Br>";
        //echo gettype($friend['uid']);
        //settype($friend['uid'],"float");
        //echo "</Br>";
        //echo gettype($friend['uid']);
     // echo strlen($count);echo '\r\n';echo $fid; exit;
      if($i == 0 && strlen($count) >= 15){
        $f_Uid = getUid($friend['pic_square']);
     /*   
      $fql_query_url = 'https://graph.facebook.com/'
    .'/fql?q=SELECT+uid,name,pic_square+FROM+user+WHERE+'//uid=me()+OR+' 
    .'uid+IN+(SELECT+uid2+FROM+friend+WHERE+uid1='.$f_Uid.')' //lay thong tin uid b?n bè
    .'&' . $access_token;
    */
/*    $fql_query_url = 'https://graph.facebook.com/'
    .'/fql?q=SELECT+id,url,pic_square,username+FROM+profile+WHERE+uid=me()'
    .'&' . $access_token;
    
      $fql_query_result = file_get_contents($fql_query_url);
    $fql_query_obj2 = json_decode($fql_query_result, true);
    $i =1; 
    /*  
      echo '<pre>';
      print_r($friend_id);
      echo '</pre>';
      */
 // }
  
  
  /*
  // Run fql multiquery
  $fql_multiquery_url = 'https://graph.facebook.com/'
    . 'fql?q={"all+friends":"SELECT+uid2+FROM+friend+WHERE+uid1=me()",'
    . '"my+name":"SELECT+name+FROM+user+WHERE+uid=me()"}'
    . '&' . $access_token;
  $fql_multiquery_result = file_get_contents($fql_multiquery_url);
  $fql_multiquery_obj = json_decode($fql_multiquery_result, true);
*/
/*
  //display results of fql multiquery
  echo '<pre>';
  print_r("multi query results:");
  print_r($fql_query_obj2);
  echo '</pre>';
  }
}
*/
/*
//dùng api
    $UID = $facebook->getAppId();
    //$fql = "SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ";
    $fql = "SELECT uid,username,first_name,middle_name,last_name,name,profile_url,about_me,quotes,sex,timezone,locale,verified,
                profile_update_time,work,education FROM user WHERE uid = 422052821143625";
        $user_info = $facebook->api(array(
                                  'method' => 'fql.query',
                                   'query' => $fql,
                                 ));
    echo '<pre>';
    print_r("query results:");
    print_r($user_info);
    echo '</pre>';
*/
/*
    $friend = "SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 30";
  $friendSQL = $facebook->fql($friend);
  foreach ($friendSQL as $friends_id){
    $uid2 = $friends_id['uid2']; 
     $query = "SELECT uid,username,name,pic_small FROM user WHERE uid='".$uid2."'";
     $friend_info = $facebook->fql($query);
    echo "<pre>";
    print_r($friend_info);
    echo "</pre>";
  }
*/
?>
<?php
function getUid($pic){
    list($link,$id,$end) = explode('_',$pic);
    return $id;
}
?>